<template>
  <div class="dashboard">
    <div class="welcome-section">
      <h1>欢迎回来，{{ userStore.user?.name || '老师' }}！</h1>
      <p class="welcome-text">今天是 {{ currentDate }}，祝您工作顺利！</p>
    </div>

    <div class="stats-section">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="stat-card" shadow="never">
            <div class="stat-content">
              <div class="stat-icon active">
                <el-icon><Briefcase /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ stats.activePositions }}</div>
                <div class="stat-label">活跃岗位</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card" shadow="never">
            <div class="stat-content">
              <div class="stat-icon pending">
                <el-icon><Document /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ stats.pendingApplications }}</div>
                <div class="stat-label">待审批申请</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card" shadow="never">
            <div class="stat-content">
              <div class="stat-icon success">
                <el-icon><User /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ stats.activeStudents }}</div>
                <div class="stat-label">在岗学生</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card" shadow="never">
            <div class="stat-content">
              <div class="stat-icon warning">
                <el-icon><Clock /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ stats.totalHours }}</div>
                <div class="stat-label">本月工时</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <div class="recent-section">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-card class="recent-card" shadow="never">
            <template #header>
              <div class="card-header">
                <span>最近申请</span>
                <el-button type="text" size="small" @click="$router.push('/teacher/approve')">
                  查看全部
                </el-button>
              </div>
            </template>
            <div class="recent-list">
              <div v-for="application in recentApplications" :key="application.id" class="recent-item">
                <div class="item-content">
                  <div class="item-info">
                    <div class="item-title">{{ application.positionName }}</div>
                    <div class="item-student">{{ application.studentName }}</div>
                  </div>
                  <div class="item-status">
                    <el-tag :type="getStatusType(application.status)" size="small">
                      {{ getStatusText(application.status) }}
                    </el-tag>
                  </div>
                </div>
                <div class="item-time">{{ application.applyTime }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="12">
          <el-card class="recent-card" shadow="never">
            <template #header>
              <div class="card-header">
                <span>工时记录</span>
                <el-button type="text" size="small" @click="$router.push('/teacher/attendance')">
                  查看全部
                </el-button>
              </div>
            </template>
            <div class="recent-list">
              <div v-for="record in recentRecords" :key="record.id" class="recent-item">
                <div class="item-content">
                  <div class="item-info">
                    <div class="item-title">{{ record.positionName }}</div>
                    <div class="item-student">{{ record.studentName }}</div>
                  </div>
                  <div class="item-hours">{{ record.hours }}小时</div>
                </div>
                <div class="item-time">{{ record.workDate }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <div class="quick-tasks">
      <el-card class="tasks-card" shadow="never">
        <template #header>
          <div class="card-header">
            <span>待办事项</span>
          </div>
        </template>
        <div class="tasks-list">
          <div v-for="task in quickTasks" :key="task.id" class="task-item" @click="handleTaskClick(task)">
            <div class="task-icon">
              <el-icon><component :is="task.icon" /></el-icon>
            </div>
            <div class="task-content">
              <div class="task-title">{{ task.title }}</div>
              <div class="task-desc">{{ task.description }}</div>
            </div>
            <div class="task-count" v-if="task.count">
              <el-badge :value="task.count" type="danger" />
            </div>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { 
  Briefcase, Document, User, Clock, Check, Edit, Money 
} from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()

// 当前日期
const currentDate = computed(() => {
  const now = new Date()
  return now.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    weekday: 'long'
  })
})



// 待办事项
const quickTasks = ref([
  {
    id: 1,
    title: '审批申请',
    description: '有8个学生申请待审批',
    icon: 'Check',
    count: 8,
    route: '/teacher/approve'
  },
  {
    id: 2,
    title: '发布岗位',
    description: '创建新的勤工助学岗位',
    icon: 'Edit',
    route: '/teacher/post'
  },
  {
    id: 3,
    title: '工时审核',
    description: '审核学生工时记录',
    icon: 'Clock',
    count: 12,
    route: '/teacher/attendance'
  },
  {
    id: 4,
    title: '工资发放',
    description: '处理本月工资发放',
    icon: 'Money',
    route: '/teacher/salary'
  }
])

// 获取状态类型
function getStatusType(status) {
  const statusMap = {
    pending: 'warning',
    approved: 'success',
    rejected: 'danger'
  }
  return statusMap[status] || 'info'
}

// 获取状态文本
function getStatusText(status) {
  const statusMap = {
    pending: '待审核',
    approved: '已通过',
    rejected: '已拒绝'
  }
  return statusMap[status] || '未知'
}

// 处理任务点击
function handleTaskClick(task) {
  if (task.route) {
    router.push(task.route)
  }
}
</script>

<style scoped>
.dashboard {
  padding: 0;
}

.welcome-section {
  margin-bottom: 32px;
  text-align: center;
}

.welcome-section h1 {
  font-size: 28px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 8px 0;
}

.welcome-text {
  font-size: 16px;
  color: #909399;
  margin: 0;
}

.stats-section {
  margin-bottom: 32px;
}

.stat-card {
  border: none;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.04);
}

.stat-content {
  display: flex;
  align-items: center;
  padding: 20px;
  gap: 16px;
}

.stat-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: #fff;
}

.stat-icon.active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.stat-icon.pending {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.stat-icon.success {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.stat-icon.warning {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-info {
  flex: 1;
}

.stat-number {
  font-size: 28px;
  font-weight: 700;
  color: #303133;
  line-height: 1;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  font-weight: 500;
}

.recent-section {
  margin-bottom: 32px;
}

.recent-card {
  border: none;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.04);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  color: #303133;
}

.recent-list {
  padding: 16px 0;
}

.recent-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.recent-item:last-child {
  border-bottom: none;
}

.item-content {
  flex: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.item-info {
  flex: 1;
}

.item-title {
  font-size: 14px;
  color: #303133;
  font-weight: 500;
  margin-bottom: 4px;
}

.item-student {
  font-size: 12px;
  color: #909399;
}

.item-status {
  margin-left: 16px;
}

.item-hours {
  font-size: 14px;
  color: #409EFF;
  font-weight: 500;
  margin-left: 16px;
}

.item-time {
  font-size: 12px;
  color: #909399;
  margin-left: 16px;
}

.quick-tasks {
  margin-bottom: 32px;
}

.tasks-card {
  border: none;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.04);
}

.tasks-list {
  padding: 16px 0;
}

.task-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
  border-bottom: 1px solid #f0f0f0;
}

.task-item:last-child {
  border-bottom: none;
}

.task-item:hover {
  background: #f5f7fa;
  transform: translateX(4px);
}

.task-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: #fff;
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.task-content {
  flex: 1;
}

.task-title {
  font-size: 14px;
  color: #303133;
  font-weight: 500;
  margin-bottom: 4px;
}

.task-desc {
  font-size: 12px;
  color: #909399;
}

.task-count {
  margin-left: 16px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .stats-section .el-col,
  .recent-section .el-col {
    margin-bottom: 16px;
  }
  
  .welcome-section h1 {
    font-size: 24px;
  }
  
  .stat-number {
    font-size: 24px;
  }
}
</style> 